import { fetchGet } from "./fetch"
import { API_URL } from "../consts/apiUrl"

// SUITE
export const getAllSuites = async () => {
  const result = await fetchGet(API_URL.getAllSuites, {})
  return result.data
}

export const getAllSuitesOfPage = async (page, opt) => {
  const result = await fetchGet(API_URL.getAllSuitesOfPage, { page }, opt)
  return result.data
}

export const getAllSuitesOfPlan = async (planId, opt) => {
  const result = await fetchGet(API_URL.getAllSuitesOfPlan, { planId }, opt)
  return result.data
}

export const getAllStepsOfSuiteInPlan = async ({ planId, suiteId }, opt) => {
  const result = await fetchGet(API_URL.getAllStepsOfSuiteInPlan, { planId, suiteId }, opt)
  return result.data
}

export const getSuiteDetail = async id => {
  const result = await fetchGet(API_URL.getSuiteDetail, { id })
  return result.data
}

export const getFlatSuiteDetail = async id => {
  const result = await fetchGet(API_URL.getSuiteDetail, { id })
  const stepList = result.data

  for (let i = 0; i < stepList.length; i++) {
    const step = stepList[i]
    if (step.code === 'stepCollection') {
      const stepCollectionId = step.data.stepCollectionId

      // 将 step collection 替换成对应的 step 明细
      const detailList = await getStepCollectionDetail(stepCollectionId)
      stepList.splice(i, 1, ...detailList.map(item => ({ ...item, suiteId: id })))
      i += (detailList.length - 1)
    }
  }

  return stepList
}

// STEP COLLECTION
export const getAllStepCollections = async () => {
  const result = await fetchGet(API_URL.getAllStepCollections, {})
  return result.data
}

export const getStepCollectionDetail = async id => {
  const result = await fetchGet(API_URL.getStepCollectionDetail, { id })
  return result.data
}

// SCREENSHOT
export const getScreenShotOfStep = async ({ planId, stepId, screenshotId }) => {
  const result = await fetchGet(API_URL.getScreenShotOfStep, { planId, stepId, screenshotId })
  return result
}

// ELEMENT
export const getAllElementsOfPage = async (page, opt) => {
  const result = await fetchGet(API_URL.getAllElementsOfPage, { page }, opt)
  return result.data.map(item => ({ ...item, mode: '' }))
}

export const getAllElementsOfPageAndNode = async (page, node) => {
  const allElementsOfPage = await getAllElementsOfPage(page)
  if (node && node.name) {
    const allNodesNames = []    //传过来的node的子项也需要显示关联的元素
    const addToNames = parentNode => {
      allNodesNames.push(parentNode.name)

      if (parentNode.children && parentNode.children.length) {
        for (let i = 0; i < parentNode.children.length; i++) {
          const child = parentNode.children[i]
          addToNames(child)
        }
      }
    }

    addToNames(node)
    return allElementsOfPage.filter(el => !el.node || allNodesNames.includes(el.node))
  } else {
    return allElementsOfPage
  }
}

export const getElementsOfIds = async ids => {
  const result = await fetchGet(API_URL.getElementsOfIds, { ids })
  return result.data
}

// AJAX
export const getAllAjaxsOfPage = async page => {
  const result = await fetchGet(API_URL.getAllAjaxsOfPage, { page })
  return result.data.map(item => ({ ...item, mode: '' }))
}

export const getAjaxsOfIds = async ids => {
  const result = await fetchGet(API_URL.getAjaxsOfIds, { ids })
  return result.data
}

// MOCK
export const getAllMocksOfAjax = async id => {
  const result = await fetchGet(API_URL.getAllMocksOfAjax, { id })
  return result.data.map(item => ({ ...item, mode: '' }))
}

export const getMocksOfIds = async ids => {
  const result = await fetchGet(API_URL.getMocksOfIds, { ids })
  return result.data
}

// CONFIG
export const getAllConfigs = async () => {
  const result = await fetchGet(API_URL.getAllConfigs)
  return result.data
}

// LOG
export const statLogBySuite = async () => {
  const result = await fetchGet(API_URL.statLogBySuite)
  return result.data
}

export const statLogsOfPlan = async (planId, opt) => {
  const result = await fetchGet(API_URL.statLogsOfPlan, { planId }, opt)
  return result.data
}

export const getStepFullLog = async ({ planId, stepId }) => {
  const result = await fetchGet(API_URL.getStepFullLog, { planId, stepId })
  return result.data
}

export const getLogsOfSuite = async ({ planId, suiteId }, opt) => {
  const result = await fetchGet(API_URL.getLogsOfSuite, { planId, suiteId }, opt)
  return result.data
}

// PLAN
export const getAllPlans = async () => {
  const result = await fetchGet(API_URL.getAllPlans)
  return result.data
}

export const getAllPlanSummaries = async () => {
  const result = await fetchGet(API_URL.getAllPlanSummaries)
  return result.data
}